package edu.scau.misp.backend_module.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import edu.scau.misp.backend_module.core.domain.AjaxResult;
import edu.scau.misp.backend_module.domain.Deal;
import edu.scau.misp.backend_module.domain.Order;
import edu.scau.misp.backend_module.domain.OrderDeal;
import edu.scau.misp.backend_module.service.IDealService;
import edu.scau.misp.backend_module.service.OrderDealService;
import edu.scau.misp.backend_module.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"order"})
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDealService orderDealService;

    @GetMapping("/sn/{dealSn}")
    public AjaxResult getDishBySn(@PathVariable("dealSn") Long dealSn) {
        return AjaxResult.success(orderService.getOrderById(dealSn));
    }

    @GetMapping("/listAll")
    public AjaxResult listAllDeal(){
        return AjaxResult.success(orderService.listAllOrder());
    }

    @GetMapping("/list")
    public AjaxResult list(@RequestParam(required = false)  Long orderId) {
        List<OrderDeal> orderDealById = orderDealService.getOrderDealById(orderId);
        return AjaxResult.success(orderDealById);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResult add(@RequestBody String  json) {
        // fastjson转成json对象
        JSONObject jsonObject = JSON.parseObject(json);
        // 在转成不同的实体类
        Order order = jsonObject.getJSONObject("order").toJavaObject(Order.class);
        order.setPaymentStatus("待支付");
        order.setPaymentMethod("暂无");
        order.setOrderDate(new Date());
        order.setIsOut("正在备餐...");
        order.setTotalPrice(jsonObject.getIntValue("totalPrice"));
        List<OrderDeal> orderDeal = jsonObject.getJSONArray("orderDeal").toJavaList(OrderDeal.class);

        int i1 = orderService.insertOrder(order);
        if (i1 > 0) {
            int id = orderService.getId().intValue();
            if (orderDeal.size() > 0) {
                for(int i = 0;i<orderDeal.size();i++) {
                    OrderDeal orderDeal1 = orderDeal.get(i);
                    orderDeal1.setOrderId(id);
                    orderDealService.insertOrderDeal(orderDeal1);
                }

            }
        }
        return i1 > 0 ? AjaxResult.success("添加套餐成功") : AjaxResult.error("添加套餐失败");
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult edit(@RequestParam(required = false)  Long orderId) {
        Order order = new Order();
        order.setOrderId(orderId);
        order.setIsOut("已出餐");
        int rows = orderService.updateOrder(order);
        return rows > 0 ? AjaxResult.success("修改套餐成功") : AjaxResult.error("修改套餐失败");
    }
    @DeleteMapping("/del")
    public AjaxResult remove(@RequestParam(required = false)  Long orderId) {
        int rows = orderService.deleteOrderById(orderId);
        orderDealService.deleteOrderDealById(orderId);
        return rows > 0 ? AjaxResult.success("删除套餐成功") : AjaxResult.error("删除套餐失败");
    }

    @RequestMapping(value = "/updateByPay", method = RequestMethod.POST)
    public AjaxResult updateByPay(@RequestBody String  json) {
        // fastjson转成json对象
        JSONObject jsonObject = JSON.parseObject(json);
        // 在转成不同的实体类
        String payMethod = jsonObject.getString("payMethod");
        Long orderId = jsonObject.getLong("orderId");
        Order order = new Order();
        order.setOrderId(orderId);
        order.setPaymentMethod(payMethod);
        order.setPaymentStatus("已支付");
        order.setPaymentTime(new Date());
        int rows = orderService.updateOrderByPay(order);
        return rows > 0 ? AjaxResult.success("修改套餐成功") : AjaxResult.error("修改套餐失败");
    }


}
